package day14;

import day4.BankAcc;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author oldliu
 * @since 1.0
 */
public class FileServer {
    public static void main(String[] args) {
        ServerSocket st = null;
        try {
            st = new ServerSocket(9999);
            while (true) {
                //new SendThread(st.accept()).start();
                //new Thread2(st.accept()).start();
                new ObjectThread(st.accept()).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

class SendThread extends Thread {
    private final Socket s;

    public SendThread(Socket s) {
        this.s = s;
    }

    public static final String fileName = "D:\\Documents\\图片和资料\\job-salary.png";

    @Override
    public synchronized void start() {
        super.start();
    }

    @Override
    public void run() {
        System.out.println(s+"链接了");
        try {
            FileInputStream fin = new FileInputStream(fileName);
            byte[] b = new byte[fin.available()];
            int leng = fin.read(b);
            fin.close();
            OutputStream out = s.getOutputStream();
            out.write(b, 0, leng);
            out.flush();
            System.out.println("文件发送给" + s + "成功");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                s.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}

//D:\Documents\图片和资料
class Thread2 extends Thread{
    private final Socket s;

    public Thread2(Socket s) {
        this.s = s;
    }
    public static final String FOLDER="D:\\Documents\\图片和资料";
    @Override
    public void run() {
        System.out.println(s+"connect。。。");
        OutputStream out=null;
        try {
            InputStream in = s.getInputStream();
             out = s.getOutputStream();
            BufferedReader br=new BufferedReader(new InputStreamReader(in));
            String fileName=br.readLine();
            FileInputStream fin=new FileInputStream(new File(FOLDER,fileName));
            byte[] b = new byte[fin.available()];
            int leng = fin.read(b);
            fin.close();
            //补一个字节
            out.write(1);
            out.write(b, 0, leng);
            out.flush();
            System.out.println("文件"+fileName+"发送给" + s + "成功");

        } catch (IOException e) {
            try {
                out.write(0);
            } catch (Exception e1) {

            }
            System.out.println(s+":"+e);
            e.printStackTrace();
        } finally {
            try {
                s.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

class ObjectThread extends  Thread{
    private final Socket s;
    private static Map<String,Object> m=new HashMap<>();
    static {
        m.put("最帅","陈毅");
        m.put("最美","马晓波");
        m.put("最高","叶树松");
        m.put("最矮","老刘");
        m.put("最胖","马晓波");
        m.put("richman",new BankAcc("an0001","老吴","合肥","10001",55555555.5F));
    }
    public ObjectThread(Socket s){
        this.s=s;
    }

    @Override
    public void run() {
        try {
            InputStream in = s.getInputStream();
            OutputStream out = s.getOutputStream();
            ObjectOutputStream oout=new ObjectOutputStream(out);
            ObjectInputStream oin=new ObjectInputStream(in);
            m.put("time",new Date());

            oout.writeObject(m);
            oout.reset();//重置，防止他缓存
            System.out.println("发送信息到"+s+"成功");
            Object o = oin.readObject();
            System.out.println("从"+s+" 收到了"+o);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                s.close();
            } catch (Exception e) {

            }
        }
    }
}